{
 "cells": [
  {
   "cell_type": "markdown",
   "source": [
    "## 1\r\n",
    "\r\n",
    " 给定一个长度不超过10的list，输出其所有的子集，如输入X=[1,2,3,4]，输出[],[1],[2],[3],[4],[1,2],[2,3],[3,4],[1,2,3],[2,3,4],[1,2,3,4]......（空集也是子集）\r\n"
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [
    "# -*- coding:utf-8 -*-\r\n",
    "# Author: Bernard\r\n",
    "# Date: 2021-7-14\r\n",
    "\r\n",
    "def printSubSet(arr):\r\n",
    "    ans = []\r\n",
    "    sz = len(arr)\r\n",
    "    for l in range(sz):\r\n",
    "        for r in range(l, sz):\r\n",
    "            ans.append(arr[l:r+1])\r\n",
    "    print(ans)\r\n",
    "\r\n",
    "\r\n",
    "printSubSet([1, 2, 3, 4])"
   ],
   "outputs": [],
   "metadata": {}
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 2\r\n",
    "\r\n",
    "给出矩阵的长和宽，打印出一个蛇形数字矩阵（从1开始递增的自然数）。\r\n",
    "输入\r\n",
    "```\r\n",
    "2 2\r\n",
    "```\r\n",
    "输出\r\n",
    "```\r\n",
    "1 2\r\n",
    "4 3\r\n",
    "```\r\n",
    "\r\n",
    "输入\r\n",
    "```\r\n",
    "3 3\r\n",
    "```\r\n",
    "输出\r\n",
    "```\r\n",
    "1 2 3\r\n",
    "8 9 4\r\n",
    "7 6 5\r\n",
    "```"
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [
    "# -*- coding:utf-8 -*-\r\n",
    "# Author: Bernard\r\n",
    "# Date: 2021-7-14\r\n",
    "\r\n",
    "def printSnakeMat(n, m):\r\n",
    "    # init\r\n",
    "    a = [[0]*m for _ in range(n)]\r\n",
    "\r\n",
    "    # process\r\n",
    "    x, y, p = 0, 0, 0\r\n",
    "    dx = [0, 1, 0, -1]\r\n",
    "    dy = [1, 0, -1, 0]\r\n",
    "    # R D L U\r\n",
    "    for i in range(1, n*m+1):\r\n",
    "        xx, yy = x+dx[p], y+dy[p]  # next pos\r\n",
    "        if xx < 0 or xx >= n or yy < 0 or yy >= m or a[xx][yy] != 0:\r\n",
    "            p = (p+1) % 4  # change direction\r\n",
    "            xx, yy = x+dx[p], y+dy[p]\r\n",
    "        a[x][y] = i\r\n",
    "        x, y = xx, yy\r\n",
    "\r\n",
    "    # print\r\n",
    "    for line in a:\r\n",
    "        for val in line:\r\n",
    "            print(val, end='\\t')\r\n",
    "        print('')\r\n",
    "\r\n",
    "\r\n",
    "while True:\r\n",
    "    n, m = map(int, input().split())\r\n",
    "    printSnakeMat(n, m)"
   ],
   "outputs": [],
   "metadata": {}
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 3\r\n",
    "\r\n",
    "给出学生总数，每个学生的学号，姓名和分数，按分数排列名次，分数相同时，按学号字典序排列名次\r\n",
    "输入 \r\n",
    "```\r\n",
    "3\r\n",
    "20162670 张三 89\r\n",
    "20162671 李四 89\r\n",
    "20162672 王五 90\r\n",
    "```\r\n",
    "输出\r\n",
    "```\r\n",
    "学号 \t \t姓名\t成绩\t名次\r\n",
    "20162672 \t王五\t 90\t\t1\r\n",
    "20162670 \t张三\t 89\t\t2\r\n",
    "20162671 \t李四\t 89\t\t3\r\n",
    "```"
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [
    "# -*- coding:utf-8 -*-\r\n",
    "# Author: Bernard\r\n",
    "# Date: 2021-7-14\r\n",
    "\r\n",
    "class stu():\r\n",
    "    def __init__(self, a):\r\n",
    "        self.idx = int(a[0])\r\n",
    "        self.name = a[1]\r\n",
    "        self.score = int(a[2])\r\n",
    "\r\n",
    "    def __lt__(self, other):\r\n",
    "        return self.score < other.score\r\n",
    "\r\n",
    "    def __format__(self, format_spec: str) -> str:\r\n",
    "        return str(self.idx)+'\\t'+self.name+'\\t'+str(self.score)\r\n",
    "\r\n",
    "\r\n",
    "arr = []\r\n",
    "\r\n",
    "n = int(input())\r\n",
    "for _ in range(n):\r\n",
    "    arr.append(stu(list(input().split())))\r\n",
    "\r\n",
    "arr.sort(reverse=True)\r\n",
    "\r\n",
    "print('学号\\t\\t姓名\\t成绩\\t名次')\r\n",
    "order = 1\r\n",
    "for i in arr:\r\n",
    "    print(format(i)+'\\t'+str(order))\r\n",
    "    order += 1\r\n"
   ],
   "outputs": [],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [
    "# -*- coding:utf-8 -*-\r\n",
    "# Author: Bernard\r\n",
    "# Date: 2021-7-14\r\n",
    "\r\n",
    "stuTuples = [(20162670, '张三', 89), \r\n",
    "             (20162671, '李四', 89), \r\n",
    "             (20162672, '王五', 90)]\r\n",
    "res = sorted(stuTuples, key=lambda stu: stu[2], reverse=True)\r\n",
    "print('学号\\t\\t姓名\\t成绩\\t名次')\r\n",
    "for i in range(len(res)):\r\n",
    "    for j in res[i]:\r\n",
    "        print(j, end='\\t')\r\n",
    "    print(i+1)"
   ],
   "outputs": [],
   "metadata": {}
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 4\r\n",
    "\r\n",
    "已知公元1年1月1日是星期一， 给出任意一个合理的公元后的日期，输出它是星期几？\r\n",
    "\r\n",
    "|输入       |      输出|\r\n",
    "|-|-|\r\n",
    "|2000 2 29|\t 星期二|\r\n",
    "|1900 2 28|\t 星期三|\r\n",
    "|1949 10 1|\t 星期六|\r\n",
    "|2019 6 28|\t 星期五|\r\n",
    "|2026 1 1|\t 星期四|\r\n",
    "|2050 2 28|\t 星期一|\r\n",
    "|1978 3 5|\t 星期日|"
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [
    "# -*- coding:utf-8 -*-\r\n",
    "# Author: Bernard\r\n",
    "# Date: 2021-7-14\r\n",
    "\r\n",
    "def calYearDays(year):\r\n",
    "    reapYear = year//4 - year // 100 + year // 400\r\n",
    "    return 365 * year + reapYear\r\n",
    "\r\n",
    "def cal():\r\n",
    "    year, month, day = map(int, input().split())\r\n",
    "    monDays = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]\r\n",
    "\r\n",
    "    totalDays = calYearDays(year-1) + sum(monDays[:month-1])+day-1\r\n",
    "    if (year % 4 == 0 and year % 100 != 0 or year % 400 == 0) and month > 2:\r\n",
    "        totalDays += 1\r\n",
    "    ch = ['一', '二', '三', '四', '五', '六', '日']\r\n",
    "    print('星期'+ch[totalDays % 7])\r\n",
    "\r\n",
    "while True:\r\n",
    "    cal()"
   ],
   "outputs": [],
   "metadata": {}
  }
 ],
 "metadata": {
  "orig_nbformat": 4,
  "language_info": {
   "name": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}